
package com.prabhakar.json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;



import net.sf.json.JSONObject;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class Testing_RestServices_forJSON {

	/*
	 * http://www.javacodegeeks.com/2012/09/simple-rest-client-in-java.html
	 * http://docs.objectrocket.com/java
	 */	
//	private String RestURL = "http://localhost:4040/RestProject/rest/hello/JSONRequest/";
	private static final String slash="/";
	
	public static void main(String[] args) {

		JSONObject jsonObj = createJSONObject();
	//	new Testing_RestServices_forJSON().callRestServices_PostAdd2();
		new Testing_RestServices_forJSON().callRestServices_JSON_Request(jsonObj);

	}

	private void callRestServices_PostAdd2() {

		System.out.println("Inside callRestServices_PostAdd2 ");
		String RestURL = "http://localhost:4040/RestServicesExample2/rest/Hello/add2";
	
		String num1="1";
		String num2 = "5";
		RestURL = RestURL+slash+num1+slash+num2;
		
		DefaultHttpClient client =  new DefaultHttpClient();
		HttpResponse response;
		HttpPost httpPost;
		StringEntity entity;
		try{
			httpPost = new HttpPost(RestURL);
		//	entity = new StringEntity(s);
			response = client.execute(httpPost);
			BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String res = "";
			while((res=br.readLine())!=null){
				System.out.println("res >>> "+res);
			}
		
		}catch(Exception e){
			e.printStackTrace();
		}

	}

	private void callRestServices_JSON_Request(JSONObject jsonObj) {
		
		System.out.println("Inside callRestServices_JSON_Request ");
		
		 String RestURL = "http://localhost:4040/RestServicesExample2/rest/Hello/JSONRequest/";   
		DefaultHttpClient client = new DefaultHttpClient();
		HttpResponse response;
		HttpPost httpPost;
		StringEntity entity;
		List<NameValuePair> regParams =  new ArrayList<NameValuePair>();
		try {
			
			httpPost = new HttpPost(RestURL);
			httpPost.setHeader("Accept", "application/json");
			httpPost.setHeader("Content-Type", "application/json");
			httpPost.setEntity(new StringEntity(jsonObj.toString()));
			response = client.execute(httpPost);
			BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String res = "";
			while((res=br.readLine())!=null){
				System.out.println( "res  >>>"+res);
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}catch(URISyntaxException e){
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

	private static JSONObject createJSONObject() {

		JSONObject obj = new JSONObject();
		obj.put("name", "prabhakar");
		obj.put("phone", "784578451");
		obj.put("email", "prabhakar@mail.com");

		return obj;
	}

}
